﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验报告1._2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 2,4,6,8,1,3,5,7,9 };
            InsertSort IS = new InsertSort();
            
            foreach (int number in array)
            {
                Console.Write(number);
                Console.Write("\t");
            }
            Console.Write("\n");
            Console.WriteLine("请输入需要查找的数字:");
            int num = Convert.ToInt32(Console.ReadLine());
            bool j = false ;
            for(int i=0;i<array.Length;i++)
            {
                
                if(num==array[i])
                {
                    j = true ;
                    Console.WriteLine("存在此数，位于第{0}位",i+1);break;
                }
            }
            if(j==false )
                Console.WriteLine("不存在此数");
            IS.Insert(array);
            Console.WriteLine("插入排序后的结果为：:");
            foreach (int changenum in array)
            {
                Console.Write("\t");
                Console.WriteLine(changenum);
                
            }
            Console.ReadKey();

        }
        class InsertSort
        {

            public void Insert(int[] A)
            {
                int n = A.Length;
                for (int j = 1; j < n; j++)
                {
                    int key = A[j];
                    int i = j - 1;
                    while ((i >= 0) && (A[i] > key))
                    {
                        A[i + 1] = A[i];
                        i = i - 1;
                    }
                    A[i + 1] = key;
                }
            }
        }
    }
}
